package com.kejiguan.ui;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.kejiguan.ui.adapter.ManagerUserMenuAdapter;
import com.kejiguan.ui.model.MenuItem;
import com.kejiguan.ui.model.UserManagerModer;
import com.kejiguan.ui.presenter.UserManagerPresentr;
import com.kejiguan.ui.response.ModifyUserResponse;
import com.kejiguan.ui.util.MD5Encrypt;
import com.kejiguan.ui.widget.NoScrollGridView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

/**
 * Created by YYY on 16/8/23.
 */
public class ModifyUserActivity extends Activity implements View.OnClickListener{

    private UserManagerPresentr mPresentr;

    private EditText nameInput, passwordInput, emailInput, telInput;
    private NoScrollGridView menuList;
    public View addBtn;

    private ManagerUserMenuAdapter adapter;
    private UserManagerModer preUserManagerModer;
    private List<MenuItem> menuItems;

    private String userName,userPassword,userEmail,userTel,navList;

    @Override
    public void onCreate(Bundle bundle)
    {
        super.onCreate(bundle);
        getIntentData();
        if (null==preUserManagerModer||null==menuItems)
        {
            finish();
        }
        mPresentr=new UserManagerPresentr();
        mPresentr.onCreate(ModifyUserActivity.this);
        EventBus.getDefault().register(this);

        setContentView(R.layout.activity_modify_user_info_layout);
        nameInput = (EditText) findViewById(R.id.name_input);
        passwordInput = (EditText) findViewById(R.id.password_input);
        emailInput = (EditText) findViewById(R.id.email_input);
        telInput = (EditText) findViewById(R.id.tel_input);
        menuList= (NoScrollGridView) findViewById(R.id.menu_list);
        adapter=new ManagerUserMenuAdapter(ModifyUserActivity.this,menuItems);
        menuList.setAdapter(adapter);
        addBtn=findViewById(R.id.add_user_confirm_btn);
        addBtn.setOnClickListener(this);

        fillUser();

    }


    private void getIntentData()
    {
        menuItems= (List<MenuItem>) getIntent().getSerializableExtra("menuItems");
        preUserManagerModer= (UserManagerModer) getIntent().getSerializableExtra("UserManagerModer");
    }

    private void fillUser()
    {
        nameInput.setText(preUserManagerModer.getUserName());
        telInput.setText(preUserManagerModer.getTelephone());
        emailInput.setText(preUserManagerModer.getEmail());
        adapter.setHasSelctedMenus(preUserManagerModer.getNavList());
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    @Override
    public void onClick(View v) {

        switch (v.getId())
        {
            case R.id.add_user_confirm_btn:
                userName=nameInput.getText().toString();
                userPassword=passwordInput.getText().toString();
                userEmail=emailInput.getText().toString();
                userTel=telInput.getText().toString();
                navList=adapter.getSelectMenus();

                if (TextUtils.isEmpty(userName))
                {
                    break;
                }
                if (TextUtils.isEmpty(userEmail))
                {
                    break;
                }
                if (TextUtils.isEmpty(userTel))
                {
                    break;
                }
                if (TextUtils.isEmpty(navList))
                {
                    break;
                }

                if (!userName.equals(preUserManagerModer.getUserName())||
                        !TextUtils.isEmpty(userPassword)||
                        !userEmail.equals(preUserManagerModer.getEmail())||
                        !userTel.equals(preUserManagerModer.getTelephone())||
                        !navList.equals(preUserManagerModer.getNavList()))
                {
                    UserManagerModer userManagerModer=new UserManagerModer();
                    userManagerModer.setUserName(userName);
                    if (!TextUtils.isEmpty(userPassword))
                    {
                        userManagerModer.setPassword(MD5Encrypt.MD5Encode(userPassword));
                    }else {
                        userManagerModer.setPassword(preUserManagerModer.getPassword());
                    }
                    userManagerModer.setEmail(userEmail);
                    userManagerModer.setTelephone(userTel);
                    userManagerModer.setNavList(navList);
                    userManagerModer.setUserId(preUserManagerModer.getUserId());

                    addBtn.setClickable(false);
                    mPresentr.modifyUser(userManagerModer);

                }
                break;
        }

    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(ModifyUserResponse response)
    {
        addBtn.setClickable(true);
        if ("0000".equals(response.getRetCode()))
        {
            Toast.makeText(ModifyUserActivity.this,"修改成功",Toast.LENGTH_SHORT).show();
            finish();
        }
    }
}
